package com.qqt.csr.order.service.statemachine.action.impl;

import com.qqt.csr.message.event.intrasite.IntraSiteMessageEventPublisher;
import com.qqt.csr.order.enums.WorkOrderEventEnum;
import com.qqt.csr.order.enums.WorkOrderStatusEnum;
import com.qqt.csr.order.enums.WorkOrderTemplateType;
import com.qqt.csr.order.service.event.DynamicEvent;
import com.qqt.csr.order.service.event.DynamicEventSource;
import com.qqt.csr.order.service.statemachine.action.IActionPostProcessor;
import com.qqt.csr.order.service.statemachine.context.WorkOrderContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RecoveredWorkOrderProcessor extends AbstractWorkOrderProcessor implements IActionPostProcessor {
    @Autowired
    private IntraSiteMessageEventPublisher messageEventPublisher;

    @Override
    public boolean matches(WorkOrderEventEnum event) {
        return WorkOrderEventEnum.RECOVERED.equals(event);
    }

    @Override
    public void postProcess(WorkOrderStatusEnum from, WorkOrderStatusEnum to, WorkOrderEventEnum event, WorkOrderContext context) {
        sendChangedOrderStatusMsgNotify(context, WorkOrderTemplateType.RECOVERED_WORK_ORDER, null);

        recordDynamic(context, event);
    }

    /**
     * 记录动态
     *
     * @param context
     */
    private void recordDynamic(WorkOrderContext context, WorkOrderEventEnum event) {
        DynamicEventSource eventSource = new DynamicEventSource()
                .setEvent(event)
                .setOrderId(context.getToOrder().getId());
        dynamicEventListener.onApplicationEvent(new DynamicEvent(eventSource));
    }
}
